import '../util/_base_export.dart';

class CodeBloc with ChangeNotifier {
  Map _coupon = {};
  Map get coupon => _coupon;

  void init() {
    _coupon = {};
    notifyListeners();
  }

//-----------------------------------------//

  codeQuery(String iv_code) async {
    bool lv_result = false;
    Map<String, dynamic> lm_param = {};

    lm_param = {"code": iv_code};
    // if ((iv_type ?? '').isNotEmpty) lm_param['type'] = iv_type;

    var lm_code = await AppSession.appReq.futureWrapper(
      ApiConst.httpPost,
      ApiServer.prod + ApiUri.code_query,
      {},
      lm_param,
      true,
      true,
    );

    if ((lm_code ?? {}).isNotEmpty) {
      if (lm_code['ev_error'] == 0) {
        _coupon = lm_code['ev_result'];

        lv_result = true;

        notifyListeners();
      } else if ((lm_code['ev_message'] ?? '').isNotEmpty) {
        AppPopupService.showAlertSimple('E', '出错啦', lm_code['ev_message']);
      }
    }
    return lv_result;
  }

  codeRedeem(String iv_code) async {
    bool lv_result = false;
    Map<String, dynamic> lm_param = {};

    lm_param = {"code": iv_code};
    // if ((iv_type ?? '').isNotEmpty) lm_param['type'] = iv_type;

    var lm_code = await AppSession.appReq.futureWrapper(
      ApiConst.httpPost,
      ApiServer.prod + ApiUri.code_redeem,
      {},
      lm_param,
      true,
      true,
    );

    if ((lm_code ?? {}).isNotEmpty) {
      if (lm_code['ev_error'] == 0) {
        lv_result = true;
        await AppSession.appUtil.initLoad();
      } else if ((lm_code['ev_message'] ?? '').isNotEmpty) {
        AppPopupService.showAlertSimple('E', '出错啦', lm_code['ev_message']);
      }
    }
    return lv_result;
  }
}
